//
//  NSArray+HKNetworkMethods.m
//  HKNetService
//
//  Created by 胡锦涛 on 2018/6/12.
//  Copyright © 2018年 胡锦涛. All rights reserved.
//

#import "NSArray+HKNetworkMethods.h"

@implementation NSArray (HKNetworkMethods)
-(id)objectAt:(NSUInteger)index
{
    if (index < self.count) {
        return self[index];
    }
    else
        return nil;
}

/** 字母排序之后形成的参数字符串 */
- (NSString *)HK_paramsString
{
    NSMutableString *paramString = [[NSMutableString alloc] init];
    
    NSArray *sortedParams = [self sortedArrayUsingSelector:@selector(compare:)];
    [sortedParams enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([paramString length] == 0) {
            [paramString appendFormat:@"%@", obj];
        } else {
            [paramString appendFormat:@"&%@", obj];
        }
    }];
    
    return paramString;
}

/** 数组变json */
- (NSString *)HK_arrayJsonString
{
    NSData *data=[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    return jsonStr;
}
@end
